<template>
  <vxe-modal
    title="办理任务"
    v-model="dialogVisible"
    v-if="dialogVisible"
    :loading="loading"
    width="70%" height="87vh" showFooter esc-closable show-zoom resize>

    <div style="height: 52vh; overflow-y: auto;">
      <el-card class="box-card" shadow="never" :body-style="{ padding: '10px',backgroundColor: '#bfcdda' }">
        <!-- 基本信息 -->
        <el-card class="box-card" shadow="hover">
          <div slot="header" class="clearfix">
            <span style="font-size: 16px; color: #1890ff;">基本信息</span>
          </div>
          <el-descriptions class="margin-top" :column="2" size="medium" border
                           :contentStyle="content_style" :labelStyle="label_style">
            <el-descriptions-item>
              <template slot="label">
                <div class="right-align-label">
                  <i class="el-icon-user"></i>
                  采购计划编号：
                </div>
              </template>
              {{ cgjhData.cgjhId }}
            </el-descriptions-item>
            <el-descriptions-item>
              <template slot="label">
                <div class="right-align-label">
                  <i class="el-icon-user"></i>
                  发起时间：
                </div>
              </template>
              {{ cgjhData.createTime }}
            </el-descriptions-item>
            <el-descriptions-item>
              <template slot="label">
                <div class="right-align-label">
                  <i class="el-icon-user"></i>
                  发起人：
                </div>
              </template>
              {{ cgjhData.createBy }}
            </el-descriptions-item>
          </el-descriptions>
        </el-card>
        <br>
        <!-- 采购计划明细 -->
        <el-card class="box-card" shadow="hover">
          <div slot="header" class="clearfix">
            <span style="font-size: 16px; color: #1890ff;">采购计划明细</span>
          </div>
          <el-table :header-cell-style="{background:'#d0cece',color:'#000000',fontWeight:'bold'}" height="50vh"
                    :data="cgjhMxList" ref="cgjhMx">
            <el-table-column label="序号" align="center" type="index" width="50"/>
            <el-table-column label="物资名称" align="center" prop="wzName">
            </el-table-column>
            <el-table-column label="规格型号" align="center" prop="wzGg">
            </el-table-column>
            <el-table-column label="数量" align="center" prop="sl">
            </el-table-column>
            <el-table-column label="单位" align="center" prop="wzDw">
            </el-table-column>
            <el-table-column label="专业" align="center" prop="wzZy">
            </el-table-column>
            <el-table-column label="用途" align="center" prop="yt">
            </el-table-column>
            <el-table-column label="品牌" align="center" prop="pp">
            </el-table-column>
            <el-table-column label="照片" align="center" prop="zp">
            </el-table-column>
            <el-table-column label="备注" align="center" prop="bz">
            </el-table-column>
          </el-table>
        </el-card>

      </el-card>
    </div>
    <el-divider></el-divider>

    <el-card shadow="never">
      <el-form label-width="100px">
        <el-form-item label="审核状态：">
          <el-radio label="1" v-model="submitType">同意</el-radio>
          <el-radio label="2" v-model="submitType">驳回</el-radio>
          <el-select remote v-if="submitType==='2'" v-model="denyTo" placeholder="驳回到：" @visible-change="selectBefore">
            <el-option
              v-for="item in beforeData"
              :key="item.dkey"
              :label="item.taskName"
              :value="item.dkey">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="审核意见：">
          <el-input type="textarea" v-model="comment"></el-input>
        </el-form-item>
      </el-form>
    </el-card>

    <div slot="footer" class="dialog-footer dialog-footer-center">
      <el-button type="primary" @click="onSubmit()">提交</el-button>
      <el-button @click="dialogVisible=false">关闭</el-button>
    </div>
  </vxe-modal>
</template>
<script>

import { fallBackTaskByTaskDefinitionKey, passTask, selectBefore, selectHistory} from "@/api/activiti/task"
import {getCgjh} from "@/api/wzgl/cgjh";

export default {
  name: "CgJhSq", //采购计划审批界面
  data() {
    return {
      loading: false,
      cgjhData: [], //采购计划信息
      cgjhMxList: [], //采购计划明细
      cgjhId: null, //采购计划主键ID
      beforeData: [],
      beforeDataLoading: false,
      denyTo: null,
      dialogVisible: false,
      rowData: null,
      submitType: "1",//默认同意
      comment: null,//审批意见
      businessData: null,//业务数据
      historyList: [],//历史信息
      content_style: {
        // 解决不对齐的问题
        'width': "400px",
        // 排列第二行
        'word-break': 'break-all',
        // 'background-color': '#f3f7ff'
      },
      label_style: {
        // 解决不对齐的问题
        'width': "250px",
        // 排列第二行
        'word-break': 'break-all',
      },
    };
  },
  created() {
  },
  methods: {
    handleOpen(row) {
      this.rowData = row
      this.loading = true;
      this.cgjhId = row.businessKey
      this.dialogVisible = true
      this.getDetailsData()
      setTimeout(() => {
        this.loading = false
      }, 1000)
    },
    //获取详情数据
    getDetailsData() {
      const cgjhId = this.cgjhId
      getCgjh(cgjhId).then(res => {
        this.cgjhData = res.data
        this.cgjhMxList = res.data.cgjhMxList;
      })
    },
    //获取之前的节点信息
    selectBefore(visible){
      //console.log(this.rowData.taskId, "======selectBefore")
      if (visible) {
        this.beforeDataLoading = true;
        this.denyTo = null;
        this.beforeData = [];
        selectBefore({taskId: this.rowData.taskId,
          comment: this.comment,
          businessKey: this.rowData.businessKey,
          businessType: this.rowData.businessType,
          processInstanceId: this.rowData.processInstanceId}).then(res => {
          console.table(res);
          setTimeout(() => {
            this.beforeData = res.data;
            this.beforeDataLoading = false;
          }, 100);
          //this.$options = res.data
        })
      }
    },
    clickTab(tab, event) {
      if (tab.name === 'history') {
        this.getSelectHistory()
      }
    },
    //获取历史记录
    getSelectHistory() {
      selectHistory({processInstanceId: this.rowData.processInstanceId}).then(res => {
        this.historyList = res.data
      })
    },
    //提交
    onSubmit() {
      // 打开遮罩层
      this.$modal.loading("正在提交中，请稍后...");
      if (!this.submitType) {
        this.$message.warning("请选择提交类型")
        return
      }
      if (!this.comment) {
        this.$message.warning("请输入审批意见")
        return
      }
      if (this.submitType == 1) {
        //审核通过
        passTask({
          taskId: this.rowData.taskId,
          comment: this.comment,
          params: {
            // day: this.businessData.qingJiaDay
          },
          businessKey: this.rowData.businessKey,
          businessType: this.rowData.businessType,
          processInstanceId: this.rowData.processInstanceId
        }).then(res => {
          this.dialogVisible = false
          this.$emit('ok')
          // 关闭遮罩层
          this.$modal.closeLoading();
        })
      }
      //任务驳回
      if (this.submitType == 2) {
        //审核不通过
        fallBackTaskByTaskDefinitionKey({
          comment: this.comment,
          businessKey: this.rowData.businessKey,
          businessType: this.rowData.businessType,
          processInstanceId: this.rowData.processInstanceId,
          taskId: this.rowData.taskId,
          taskDefinitionKey:this.denyTo
        }).then(res => {
          this.dialogVisible = false
          this.denyTo = null;
          this.beforeData =[];
          this.$emit('ok')
          // 关闭遮罩层
          this.$modal.closeLoading();
        })
      }
    },

  }
};
</script>
